package com.flh.crm.workbench.service.Impl;

import com.flh.crm.settings.dao.UserDao;
import com.flh.crm.settings.domain.User;
import com.flh.crm.utils.SqlSessionUtil;
import com.flh.crm.vo.PaginationVo;
import com.flh.crm.workbench.dao.ActivityDao;
import com.flh.crm.workbench.dao.ActivityRemarkDao;
import com.flh.crm.workbench.domain.Activity;
import com.flh.crm.workbench.domain.ActivityRemark;
import com.flh.crm.workbench.service.ActivityService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author FLH
 * @Date 2020/7/26 18:32
 * @Version 1.0
 */
public class ActivityServiceImpl implements ActivityService {
       private ActivityDao activityDao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);

       private ActivityRemarkDao activityRemarkDao = SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);

       private UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);

       @Override
       public boolean save(Activity activity) {

              boolean flag=true;

              int count = activityDao.save(activity);

              if (count!=1){
                flag =false;
              }

              return flag;
       }

       @Override
       public PaginationVo<Activity> pageList(Map<String, Object> map) {


              System.out.println("参数传入");
              System.out.println("name"+map.get("name"));
              System.out.println("owner"+map.get("owner"));
              System.out.println("startDate"+map.get("startDate"));
              System.out.println("endDate"+map.get("endDate"));
              System.out.println("skipCount"+map.get("skipCount"));
              System.out.println("pageSize"+map.get("pageSize"));

              //取得total
              int total = activityDao.getTotalByCondition(map);
              //取得dataList
              List<Activity> dataList = activityDao.getActivityListByCondition(map);
              //将total和dataList封装到vo
              PaginationVo<Activity> vo =new PaginationVo<Activity>();
              vo.setTotal(total);

              System.out.println("TOTAL=="+total);

              vo.setDataList(dataList);
              //将vo返回
              return vo;
       }

       @Override
       public boolean delete(String[] ids) {

              boolean flag = true;
              //查询出需要删除备注的数量
              int count1 = activityRemarkDao.getCountByAids(ids);
              //删除备注，返回受到影响的条数(实际删除的数量)
              int count2 = activityRemarkDao.deleteByAids(ids);

              if (count1!=count2){
                     flag=false;
              }
              //删除市场活动
              int count3 = activityDao.delete(ids);

              if (count3!=ids.length){
                     flag=false;
              }

              return flag;
       }

       @Override
       public Map<String, Object> getUserListAndActivity(String id) {
             //取ulist
              List<User> uList = userDao.getUserList();
              //取a
              Activity a = activityDao.getById(id);
              Map<String,Object> map = new HashMap<String, Object>();
              map.put("uList",uList);
              map.put("a",a);

             //打包map
              return map;
       }

       @Override
       public boolean update(Activity activity) {
              boolean flag = true;
              int count = activityDao.update(activity);
              if (count!=1){
                     flag=false;
              }
              return flag;
       }

       @Override
       public Activity detail(String id) {

              Activity a= activityDao.detail(id);

              return a;
       }

       @Override
       public List<ActivityRemark> getRemarkListByAid(String activityId) {

             List<ActivityRemark> arList = activityRemarkDao.getRemarkListByAid(activityId);

              return arList;
       }

       @Override
       public boolean deleteRemark(String id) {

              boolean flag = true;

              int count = activityRemarkDao.deleteById(id);

              if (count!=1){
                     flag=false;
              }
              return flag;
       }

       @Override
       public boolean saveRemark(ActivityRemark ar) {

              boolean flag = true;

              int count = activityRemarkDao.saveRemark(ar);

              if (count!=1){
                     flag = false;
              }

              return flag;
       }

       @Override
       public boolean updateRemark(ActivityRemark ar) {

              boolean flag = true;

              int count = activityRemarkDao.updateRemark(ar);

              if (count!=1){
                     flag = false;
              }

              return flag;

       }

       @Override
       public List<Activity> getActivityListByClueId(String clueId) {

              List<Activity> aList = activityDao.getActivityListByClueId(clueId);

              return aList;
       }

       @Override
       public List<Activity> getActivityListByNameAndNotByClueId(Map<String, String> map) {


              List<Activity> aList = activityDao.getActivityListByNameAndNotByClueId(map);
              return aList;
       }

       @Override
       public List<Activity> getActivityListByName(String aname) {

              List<Activity> aList = activityDao.getActivityListByName(aname);
              return aList;
       }
}
